home *** CD-ROM | disk | FTP | other *** search
/ Aminet 31 / Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso / Aminet / dev / c / vbccsrc.lha / vbcc / declaration.c < prev    next >
C/C++ Source or Header  |  1999-03-07  |  50KB  |  1,529 lines

  1. /*  $VER: vbcc (declaration.c) V0.4     */
  2.  
  3. #include "vbc.h"
  4.  
  5. static char FILE_[]=__FILE__;
  6.  
  7. #define PARAMETER 8
  8. #define OLDSTYLE 16
  9.  
  10. struct const_list *initialization(struct Typ *,int,int);
  11. int test_assignment(struct Typ *,np);
  12. int return_sc,return_reg,has_return;
  13. #ifdef HAVE_TARGET_ATTRIBUTES
  14. unsigned long return_tattr;
  15. #endif
  16.  
  17. extern int float_used;
  18. extern void optimize(long,struct Var *);
  19.  
  20. int settyp(int typnew, int typold)
  21. /* Unterroutine fuer declaration_specifiers().              */
  22. {
  23.   static int warned_long_double;
  24.   if(DEBUG&2) printf("settyp: new=%d old=%d\n",typnew,typold);
  25.   if(typold==LONG&&typnew==FLOAT){ error(203); return(DOUBLE);}
  26.   if(typold==LONG&&typnew==DOUBLE){
  27.     if(!warned_long_double){error(204);warned_long_double=1;}
  28.     return(DOUBLE);
  29.   }
  30.   if(typold!=0&&typnew!=INT){error(47);return(typnew);}
  31.   if(typold==0&&typnew==INT) return(INT);
  32.   if(typold==0) return(typnew);
  33.   if(typold==SHORT||typold==LONG) return(typold);
  34.   error(48);
  35.   return(typnew);
  36. }
  37.  
  38. #define dsc if(storage_class) error(49); if(typ||type_qualifiers) error(50)
  39. #define XSIGNED 16384
  40.  
  41. struct Typ *declaration_specifiers(void)
  42. /* Erzeugt neuen Typ und gibt Zeiger darauf zurueck,      */
  43. /* parst z.B. unsigned int, struct bla etc.               */
  44. {
  45.   int typ=0,type_qualifiers=0,notdone,storage_class,hard_reg;
  46.   char *merk,*imerk,sident[MAXI],sbuff[MAXI];
  47.   struct Typ *new=mymalloc(TYPS),*t,*ts;
  48.   struct struct_declaration *ssd;
  49.   struct struct_list (*sl)[];
  50.   size_t slsz;
  51.   struct Var *v;
  52. #ifdef HAVE_TARGET_ATTRIBUTES
  53.   unsigned long tattr=0;
  54. #endif
  55.   storage_class=hard_reg=0;
  56.   new->next=0; new->exact=0;
  57.   do{
  58.     killsp();merk=s;cpbez(buff,0);notdone=0;
  59.     if(DEBUG&2) printf("ts: %s\n",buff);
  60.     if(!strcmp("struct",buff)) notdone=STRUCT;
  61.     if(!strcmp("union",buff)) notdone=UNION;
  62.     if(notdone!=0){
  63.       killsp();
  64.       if(*s!='{'){
  65.         cpbez(sident,1);
  66.         killsp();
  67.         ssd=find_struct(sident,0);
  68.         if(ssd&&*s=='{'&&find_struct(sident,nesting)&&ssd->count>0) error(13,sident);
  69.         if(!ssd||((*s=='{'||*s==';')&&!find_struct(sident,nesting))){
  70.           typ=settyp(notdone,typ);
  71.           ssd=mymalloc(sizeof(*ssd));
  72.           ssd->count=0;
  73.           new->exact=ssd=add_sd(ssd);
  74.           add_struct_identifier(sident,ssd);
  75.         }else{
  76.           new->exact=ssd;
  77.           typ=settyp(new->flags=notdone,typ);
  78.         }
  79.       }else{
  80.         *sident=0;
  81.         typ=settyp(notdone,typ);
  82.         ssd=mymalloc(sizeof(*ssd));
  83.         ssd->count=0;
  84.         new->exact=ssd=add_sd(ssd);
  85.       }
  86.       if(*s=='{'){
  87.         s++;
  88.         killsp();
  89.         slsz=SLSIZE;
  90.         sl=mymalloc(slsz*sizeof(struct struct_list));
  91.         ssd->count=0;
  92.         imerk=ident;
  93.         ts=declaration_specifiers();
  94.         while(*s!='}'&&ts){
  95.           ident=sbuff;
  96.           t=declarator(clone_typ(ts));
  97.           killsp();
  98.           if(*s==':'){
  99.             /*  bitfields werden hier noch ignoriert    */
  100.             np tree;
  101.             if((ts->flags&NQ)!=INT) error(51);
  102.             s++;killsp();tree=assignment_expression();
  103.             if(type_expression(tree)){
  104.               if(tree->flags!=CEXPR) error(52);
  105.               if((tree->ntyp->flags&NQ)<CHAR||(tree->ntyp->flags&NQ)>LONG) error(52);
  106.             }
  107.             if(tree) free_expression(tree);
  108.           }else{
  109.             if(*ident==0) error(53);
  110.           }
  111.           if(type_uncomplete(t)){
  112.             error(14,sbuff);
  113.             freetyp(t);
  114.             break;
  115.           }
  116.           if((t->flags&NQ)==FUNKT)
  117.             error(15,sbuff);
  118.  
  119.           if(*ident!=0){
  120.             int i=ssd->count;
  121.             while(--i>=0)
  122.               if(!strcmp((*sl)[i].identifier,ident))
  123.                 error(16,ident);
  124.           }
  125.           (*sl)[ssd->count].styp=t;
  126.           (*sl)[ssd->count].identifier=add_identifier(ident,strlen(ident));
  127.           ssd->count++;
  128.           if(ssd->count>=slsz-1){
  129.             slsz+=SLSIZE;
  130.             sl=realloc(sl,slsz*sizeof(struct struct_list));
  131.             if(!sl){error(12);raus();}
  132.           }
  133.           killsp();
  134.           if(*s==',') {s++;killsp();continue;}
  135.           if(*s!=';') error(54); else s++;
  136.           killsp();
  137.           if(*s!='}'){
  138.             if(ts) freetyp(ts);
  139.             ts=declaration_specifiers();killsp();
  140.           }
  141.         }
  142.         if(ts) freetyp(ts);
  143.         if(ssd->count==0) error(55);
  144.         ident=imerk;
  145.         add_sl(ssd,sl);
  146.         free(sl);
  147.         if(*s!='}') error(56); else s++;
  148.         new->flags=notdone|type_qualifiers;
  149.       }
  150.       notdone=1;
  151.     }
  152.     if(!strcmp("enum",buff)){
  153.       /*  enumerations; die Namen werden leider noch ignoriert    */
  154.       killsp();notdone=1;
  155.       if(*s!='{'){cpbez(buff,1);killsp();}
  156.       if(*s=='{'){
  157.         zlong val; struct Var *v; struct Typ *t;
  158.         val=l2zl(0L);
  159.         s++;killsp();
  160.         while(*s!='}'){
  161.           cpbez(sident,1);killsp();
  162.           if(*sident==0) {error(56);break;}
  163.           t=mymalloc(TYPS);
  164.           t->flags=CONST|INT;
  165.           t->next=0;
  166.           if(find_var(sident,nesting)) error(17,sident);
  167.           v=add_var(sident,t,AUTO,0); /*  AUTO hier klug? */
  168.           if(*s=='='){
  169.             s++;killsp();
  170.             v->clist=initialization(v->vtyp,0,0);
  171.             val=zi2zl(v->clist->val.vint);killsp();
  172.           }else{
  173.             v->clist=mymalloc(CLS);
  174.             v->clist->val.vint=val;
  175.             v->clist->next=v->clist->other=0;
  176.             v->clist->tree=0;
  177.           }
  178.           vlong=l2zl(1L);val=zladd(val,vlong);
  179.           v->vtyp->flags=CONST|ENUM;
  180.           if(*s!='}'&&*s!=',') {error(56);break;}
  181.           if(*s==',') s++;
  182.           killsp();
  183.           if(*s=='}') {s++; break;}
  184.         }
  185.       }
  186.       killsp();
  187.       typ=settyp(INT,typ);*buff=0;
  188.     }
  189.     if(!strcmp("void",buff)) {typ=settyp(VOID,typ);notdone=1;}
  190.     if(!strcmp("char",buff)) {typ=settyp(CHAR,typ);notdone=1;}
  191.     if(!strcmp("short",buff)) {typ=settyp(SHORT,typ);notdone=1;}
  192.     if(!strcmp("int",buff)) {typ=settyp(INT,typ);notdone=1;}
  193.     if(!strcmp("long",buff)) {typ=settyp(LONG,typ);notdone=1;}
  194.     if(!strcmp("float",buff)) {typ=settyp(FLOAT,typ);notdone=1;}
  195.     if(!strcmp("double",buff)) {typ=settyp(DOUBLE,typ);notdone=1;}
  196.     if(!strcmp("const",buff)){
  197.       if(type_qualifiers&CONST) error(58);
  198.       type_qualifiers|=CONST;notdone=1;
  199.     }
  200.     if(!strcmp("volatile",buff)){
  201.       if(type_qualifiers&VOLATILE) error(58);
  202.       type_qualifiers|=VOLATILE;notdone=1;
  203.     }
  204.     if(!strcmp("unsigned",buff)){
  205.       if(type_qualifiers&(XSIGNED|UNSIGNED)) error(58);
  206.       notdone=1;type_qualifiers|=UNSIGNED;
  207.     }
  208.     if(!strcmp("signed",buff)){
  209.       if(type_qualifiers&(XSIGNED|UNSIGNED)) error(58);
  210.       notdone=1;type_qualifiers|=XSIGNED;
  211.     }
  212.     if(!strcmp("auto",buff)) {dsc;storage_class=AUTO;notdone=1;}
  213.     if(!strcmp("register",buff)){dsc;storage_class=REGISTER;notdone=1;}
  214.     if(!strcmp("static",buff)) {dsc;storage_class=STATIC;notdone=1;}
  215.     if(!strcmp("extern",buff)) {dsc;storage_class=EXTERN;notdone=1;}
  216.     if(!strcmp("typedef",buff)) {dsc;storage_class=TYPEDEF;notdone=1;}
  217.     if(!(c_flags[7]&USEDFLAG)&&!strcmp("__reg",buff)){
  218.       char *d;int f=0;
  219.       killsp(); if(*s=='(') s++; else error(151);
  220.       killsp(); if(*s=='\"') s++; else error(74);
  221.       d=buff;
  222.       while(*s&&*s!='\"'){
  223.         if(d-buff-2>MAXI){
  224.           if(!f){ error(206,MAXI);f=1;}
  225.         }else *d++=*s;
  226.         s++;
  227.       }
  228.       *d=0;
  229.       if(*s=='\"') s++; else error(74);
  230.       killsp(); if(*s==')') s++; else error(59);
  231.       for(hard_reg=1;hard_reg<=MAXR;hard_reg++){
  232.         if(!strcmp(buff,regnames[hard_reg])) break;
  233.       }
  234.       if(hard_reg>MAXR){ hard_reg=0;error(220,buff);}
  235.       notdone=1;
  236.     }
  237. #ifdef HAVE_TARGET_ATTRIBUTES
  238.     if(!(c_flags[7]&USEDFLAG)){
  239.       int i;
  240.       for(i=0;g_attr_name[i];i++){
  241.         if(!strcmp(g_attr_name[i],buff)){
  242.           if(tattr&(1L<<i)) error(227,buff);
  243.           tattr|=(1L<<i);
  244.           notdone=1;break;
  245.         }
  246.       }
  247.     }
  248. #endif
  249.  
  250.     if(!notdone&&*buff&&typ==0&&!(type_qualifiers&(XSIGNED|UNSIGNED))){
  251.       v=find_var(buff,0);
  252.       if(v&&v->storage_class==TYPEDEF){
  253.         free(new);
  254.         new=clone_typ(v->vtyp);
  255.         typ=settyp(new->flags,typ);
  256.         notdone=1;
  257.